package com.persondemo.videoappliction.ui.video;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.CollectionBean;
import com.persondemo.videoappliction.bean.VideoBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.database.CollectDao;
import com.persondemo.videoappliction.event.MainEvent;
import com.persondemo.videoappliction.ui.adapter.MoviePopAdapter;
import com.persondemo.videoappliction.ui.adapter.MyExpandableListViewAdapter;
import com.persondemo.videoappliction.ui.adapter.XianLuAdapter;
import com.persondemo.videoappliction.ui.adapter.YouLikeAdapter;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.search.SearchActivity;
import com.persondemo.videoappliction.ui.video.contract.VideoDetilContract;
import com.persondemo.videoappliction.ui.video.presenter.VodeoPresenter;
import com.persondemo.videoappliction.utils.GetSystemTimeUitl;
import com.persondemo.videoappliction.utils.ImageLoaderUtil;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.ExpandableTextView;
import com.persondemo.videoappliction.widget.NestedExpandaleListView;
import com.persondemo.videoappliction.widget.SpaceItemDecoration;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/3/20.
 */

public class VideoDetilsActivity extends BaseActivity<VodeoPresenter> implements VideoDetilContract.View {
    @BindView(R.id.rl_search)
    RelativeLayout rlSearch;
    @BindView(R.id.expand_text)
    ExpandableTextView expandText;
    @BindView(R.id.expand_list)
    NestedExpandaleListView expandList;
    @BindView(R.id.recler_xianlu)
    RecyclerView reclerXianlu;
    XianLuAdapter xianLuAdapter;
    //    @BindView(R.id.scroll_view)
//    ScrollView scrollView;
    @BindView(R.id.recycler_detils)
    RecyclerView recyclerDetils;
    @BindView(R.id.video_name)
    TextView videoName;
    @BindView(R.id.video_img_start)
    ImageView videoImgStart;
    private List<VideoBean.DataBean.LaiyuanBean> groupList;
    /**
     * 每个分组下的每个子项的 GridView 数据集合
     */
    private List<VideoBean.DataBean.LaiyuanBean> itemGridList;
    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private List<List<VideoBean.DataBean.LaiyuanBean.JishuBean>> itemList;
    private List<VideoBean.DataBean.XianluBean> xianluBeen;//线路来源
    private List<VideoBean.DataBean.LikelistBean> likelistBeen;//猜你喜欢
    private List<VideoBean.DataBean.LaiyuanBean> laiyuanBeen;//来源
    private int id = 0;
    private YouLikeAdapter youLikeAdapter;
    private String xianlu = "";
    private String laiyuan = "";
    private String jishu = "";
    MyExpandableListViewAdapter listAdapter;
    VideoBean video = new VideoBean();

    @Override
    public void onRetry() {
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_detiles;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        videoImgStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(laiyuan) || TextUtils.isEmpty(xianlu)) {
                    T.showShort(VideoDetilsActivity.this, "该视频地址无效，无法播放，请选择其他电影观看！");
                } else {
                    gowebview(xianlu + laiyuan);
                }

            }
        });
        reclerXianlu.setLayoutManager(new GridLayoutManager(VideoDetilsActivity.this, 5));
        reclerXianlu.addItemDecoration(new SpaceItemDecoration(10));
    }

    private void gowebview(String title) {
        MobclickAgent.onEvent(VideoDetilsActivity.this, "movie");
        List<CollectionBean> collectionBeanList = new ArrayList<>();
        collectionBeanList = CollectDao.getChannels();
        CollectionBean collectionBean = new CollectionBean();
        if (video == null) {
            return;
        }
        if (!TextUtils.isEmpty(GetSystemTimeUitl.getInDetailgetTime())) {
            collectionBean.setDate(GetSystemTimeUitl.getInDetailgetTime());
        }
        if (video.getData().getName() != null) {
            collectionBean.setTitle(video.getData().getName());
        } else {
            collectionBean.setTitle("");
        }
        collectionBean.setVodeoid(video.getData().getId());
        if (video.getData().getImgs() != null) {
            collectionBean.setImg(video.getData().getImgs());
        } else {
            collectionBean.setImg("");
        }
        collectionBean.setLaji(jishu);
        collectionBean.setTime(video.getData().getContent());
        CollectDao.quchong(collectionBean);
        Intent intent = new Intent(VideoDetilsActivity.this, WeBviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void initData() {
        xianluBeen = new ArrayList<>();
        likelistBeen = new ArrayList<>();
        // 每个分组下的每个子项的 GridView 数据集合
        itemGridList = new ArrayList<>();
        laiyuanBeen = new ArrayList<>();
        itemList = new ArrayList<>();
        // 分组
        groupList = new ArrayList<>();
        Intent intent = getIntent();
        if (intent != null) {
            showLoadingDialog();
            id = intent.getIntExtra("id", 0);
            mPresenter.getdata(id);
        }
        rlSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(VideoDetilsActivity.this, SearchActivity.class));
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });
    }

    @Subscriber
    private void godata(MainEvent event) {
        if (event == null) return;
        laiyuan = event.title;
        jishu = event.jishu;
        if (!TextUtils.isEmpty(xianlu) && !TextUtils.isEmpty(laiyuan)) {
            gowebview(xianlu + laiyuan);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void loaddata(VideoBean videoBean) {
        video = videoBean;
        hideLoadingDialog();
        if (videoBean.getStatus() == 1) {
            showSuccess();

            if (videoBean.getData().getLikelist() != null && videoBean.getData().getLikelist().size() > 0) {
                likelistBeen.clear();
                recyclerDetils.setLayoutManager(new GridLayoutManager(VideoDetilsActivity.this, 3));
                youLikeAdapter = new YouLikeAdapter(likelistBeen, VideoDetilsActivity.this);
                likelistBeen = videoBean.getData().getLikelist();
                youLikeAdapter.setNewData(likelistBeen);
                recyclerDetils.setAdapter(youLikeAdapter);
                youLikeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        showLoadingDialog();
                        mPresenter.getdata(likelistBeen.get(position).getId());
                        xianluBeen.clear();
                        xianLuAdapter = new XianLuAdapter(xianluBeen, VideoDetilsActivity.this);
                        xianLuAdapter.notifyDataSetChanged();
                        likelistBeen.clear();
                        youLikeAdapter.notifyDataSetChanged();
                        itemList.clear();
                        groupList.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                });
            }
            if (!TextUtils.isEmpty(videoBean.getData().getContent())) {
                expandText.setText(videoBean.getData().getContent());
            }
            if (videoBean.getData().getXianlu() != null && videoBean.getData().getXianlu().size() > 0) {
                xianluBeen.clear();
                xianluBeen = videoBean.getData().getXianlu();
                xianLuAdapter = new XianLuAdapter(xianluBeen, VideoDetilsActivity.this);
                reclerXianlu.setAdapter(xianLuAdapter);
                xianLuAdapter.setOnItemActionListener(new MoviePopAdapter.OnItemActionListener() {
                    @Override
                    public void onItemClickListener(View v, int pos) {
                        xianlu = xianluBeen.get(pos).getUrl();
                        if (TextUtils.isEmpty(laiyuan) || TextUtils.isEmpty(xianlu)) {
                            T.showShort(VideoDetilsActivity.this, "该视频地址无效，无法播放，请选择其他电影观看！");
                        } else {
                            gowebview(xianlu + laiyuan);
                        }
                    }
                });
            }
            if (videoBean.getData().getLaiyuan() != null && videoBean.getData().getLaiyuan().size() > 0) {
                laiyuanBeen.clear();
                laiyuanBeen = videoBean.getData().getLaiyuan();
                for (int i = 0; i < laiyuanBeen.size(); i++) {
                    groupList.add(laiyuanBeen.get(i));
                    for (int j = 0; j < laiyuanBeen.get(i).getJishu().size(); j++) {
                        itemGridList.add(laiyuanBeen.get(i));
                        itemList.add(itemGridList.get(j).getJishu());
                    }
                }
            }
            if (!TextUtils.isEmpty(videoBean.getData().getName())) {
                videoName.setText("影片名：" + videoBean.getData().getName());
            }
            ImageLoaderUtil.LoadImage(VideoDetilsActivity.this, videoBean.getData().getImgs(), videoImgStart);
            // 创建适配器
            if (groupList != null || itemList != null) {
                listAdapter = new MyExpandableListViewAdapter(VideoDetilsActivity.this,
                        groupList, itemList);
                expandList.setAdapter(listAdapter);
                // 隐藏分组指示器
                expandList.setGroupIndicator(null);
                // 默认展开第一组
                expandList.expandGroup(0);
                expandList.setNestedScrollingEnabled(true);
            }
            if (videoBean.getData().getLaiyuan().size() != 0) {
                laiyuan = videoBean.getData().getLaiyuan().get(0).getJishu().get(0).getUrls();
            } else {
                T.showShort(VideoDetilsActivity.this, "该视频地址无效，无法播放，请选择其他电影观看！");
            }

            if (videoBean.getData().getXianlu().size() != 0) {
                xianlu = videoBean.getData().getXianlu().get(0).getUrl();
            }

        } else {
            T.showShort(VideoDetilsActivity.this, videoBean.getMsg());
        }
    }


    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}